<?php

namespace app\backstage\controller;


use app\model\TaskType as model;
use app\util\ReturnCode;
use think\Db;

class TaskType extends Base {

    public function index(){
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $postData = $this->request->get();
        $where = function($query)use($postData){
            if(isset($postData['keyword'])&& $postData['keyword']){
                $query->where('name',  'like','%'.$postData['keyword'].'%');
            }

        };

        

        $list = model::where($where)
        ->order('addTime desc')
        ->limit(( $page - 1 ) * $size , $size)
        ->select();

        $count =   model::where($where)
        ->count();

        return $this->buildSuccess([
            'count'=>$count,
            'list'=>$list
        ]);
    }

    public function addOrUpd(){

        $postData = $this->request->post();
        if(!isset($postData['name']) || empty($postData['name'])){
            return $this->buildFailed(ReturnCode::INVALID, '任务标题为空');
        }
        if(mb_strlen($postData['name']) > 200){
            return $this->buildFailed(ReturnCode::INVALID, '任务标题大于200字符');
        }
        $data = [
            'name' => $postData['name'],
        ];
        if(isset($postData['id']) && $postData['id']){
            $data['updateTime'] = time();
            $res = model::where('id', $postData['id'])->update($data);
        }else{
            $res = model::create($data);
        }
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }

    }

    public function del(){
        $postData = $this->request->post();
        if(!isset($postData['id']) || empty($postData['id'])){
            return $this->buildFailed(ReturnCode::INVALID, 'id为空');
        }
        $res = model::where('id', $postData['id'])->delete();
        if($res > 0){
            return $this->buildSuccess([]);
        }
        return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
    }



}

?>
